/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.minierp.bean.MakeTypeComparator;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.MakeTypeDao;
import com.minierp.dao.WorkCenterDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.MakeType;
import com.minierp.model.WorkCenter;
import com.minierp.view.MakeTypeListExcelView;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class MakeTypeMACtr extends MultiActionController {
	private MakeTypeDao makeTypeDao;
	private WorkCenterDao workCenterDao;
	private IdGenerator idGenerator;
	private String listView;
	private String listForSelectView;
	private String listForMultiSelectView;
	private String impoFormView;

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public String getImpoFormView() {
		return this.impoFormView;
	}

	public void setImpoFormView(String impoFormView) {
		this.impoFormView = impoFormView;
	}

	public String getListForMultiSelectView() {
		return this.listForMultiSelectView;
	}

	public void setListForMultiSelectView(String listForMultiSelectView) {
		this.listForMultiSelectView = listForMultiSelectView;
	}

	public ModelAndView handleList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "MAKE_TYPE")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			ModelAndView mv = new ModelAndView(this.listView);
			String parentId = request.getParameter("parent_id");
			String contentType = request.getParameter("content_type");
			if (parentId == null || parentId.equals("")) {
				parentId = "0";
			}

			List wcList = this.workCenterDao.getList(false);
			if (contentType != null && contentType.equals("EXCEL")) {
				List list3 = this.makeTypeDao.getList(true);
				Object[] list1 = this.sortMt(list3);
				HashMap map = new HashMap();
				map.put("list", list1);
				map.put("mtList", list3);
				map.put("wcList", wcList);
				MakeTypeListExcelView v = new MakeTypeListExcelView();
				return new ModelAndView(v, map);
			} else {
				if (!parentId.equals("0")) {
					MakeType list = (MakeType) this.makeTypeDao.get(
							MakeType.class, Long.parseLong(parentId));
					if (list != null) {
						mv.addObject("retId",
								String.valueOf(list.getParentId()));
					}
				}

				Object[] list2 = this.sortMt(this.makeTypeDao.getList(
						Long.parseLong(parentId), true));
				mv.addObject("list", list2);
				mv.addObject("makeTypeList", this.makeTypeDao.getList(true));
				mv.addObject("parentId", parentId);
				mv.addObject("workCenterList", wcList);
				return mv;
			}
		}
	}

	public ModelAndView handleDelete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String returnUrl = request.getParameter("from_url");
		ModelAndView mv = new ModelAndView("redirect:" + returnUrl);
		long id = Long.parseLong(request.getParameter("id"));
		this.makeTypeDao.unenabledTree(id);
		EncCommonHelper.setAlertMessage(request, "删除成功！");
		return mv;
	}

	public ModelAndView handleListForSelect(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForSelectView);
		mv.addObject("makeTypeList",
				this.sortMt(this.makeTypeDao.getList(true)));
		mv.addObject("workCenterList", this.workCenterDao.getList(false));
		return mv;
	}

	public ModelAndView handleListForMultiSelect(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForMultiSelectView);
		String searchKey = request.getParameter("search_key");
		mv.addObject("makeTypeList",
				this.sortMt(this.makeTypeDao.getList(searchKey)));
		mv.addObject("workCenterList", this.workCenterDao.getList(false));
		mv.addObject("searchKey", searchKey);
		return mv;
	}

	private Object[] sortMt(List list) {
		Object[] objs = list.toArray();
		Arrays.sort(objs, new MakeTypeComparator());
		return objs;
	}

	public ModelAndView handleImpoForm(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView(this.impoFormView);
	}

	public ModelAndView handleImpo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String fileName = request.getParameter("file_name");
		File file = new File(this.getServletContext().getRealPath(fileName));
		String alertMsg = "";
		List wcList = this.workCenterDao.getList(true);
		List mtList = this.makeTypeDao.getList(true);
		HashMap addMap = new HashMap();
		BufferedInputStream in = null;

		try {
			in = new BufferedInputStream(new FileInputStream(file));
			HSSFWorkbook out = new HSSFWorkbook(new POIFSFileSystem(in));
			int count = 0;

			for (int str = 0; str < out.getNumberOfSheets(); ++str) {
				HSSFSheet hssfsheet = out.getSheetAt(str);

				for (int j = 1; j < hssfsheet.getPhysicalNumberOfRows(); ++j) {
					HSSFRow hssfrow = hssfsheet.getRow(j);
					if (hssfrow == null) {
						break;
					}

					boolean hasInput = false;
					HSSFCell cell = hssfrow.getCell(0);
					String sequCode = PageHelper.getCellVal(cell, 25);
					hasInput = hasInput || !PageHelper.isEmpty(sequCode);
					cell = hssfrow.getCell(1);
					String pareName = PageHelper.getCellVal(cell, 0);
					hasInput = hasInput || !PageHelper.isEmpty(pareName);
					cell = hssfrow.getCell(2);
					String mtName = PageHelper.getCellVal(cell, 15);
					hasInput = hasInput || !PageHelper.isEmpty(mtName);
					cell = hssfrow.getCell(3);
					String wcName = PageHelper.getCellVal(cell, 0);
					hasInput = hasInput || !PageHelper.isEmpty(wcName);
					if (hasInput) {
						long wcId = 0L;
						long pareId = 0L;
						String pareIdStr = "";
						boolean dataOk = true;
						if (dataOk && (sequCode == null || sequCode.equals(""))) {
							alertMsg = PageHelper.appendAlertMsg(alertMsg, "第"
									+ j + "行：序号不能为空！");
							dataOk = false;
						}

						boolean mt;
						int k;
						if (dataOk && pareName != null && !pareName.equals("")) {
							mt = false;
							MakeType wc = null;
							if (addMap.containsKey(pareName)) {
								wc = (MakeType) addMap.get(pareName);
								mt = true;
								pareId = wc.getId();
								if (wc.getParentIdStr() != null
										&& !wc.getParentIdStr().equals("")) {
									pareIdStr = wc.getParentIdStr()
											+ wc.getId() + ";";
								} else {
									pareIdStr = ";" + wc.getId() + ";";
								}
							} else {
								for (k = 0; k < mtList.size(); ++k) {
									wc = (MakeType) mtList.get(k);
									if (wc.getTypeName().equals(pareName)) {
										mt = true;
										break;
									}
								}
							}

							if (mt) {
								pareId = wc.getId();
								if (wc.getParentIdStr() != null
										&& !wc.getParentIdStr().equals("")) {
									pareIdStr = wc.getParentIdStr()
											+ wc.getId() + ";";
								} else {
									pareIdStr = ";" + wc.getId() + ";";
								}
							} else {
								alertMsg = PageHelper.appendAlertMsg(alertMsg,
										"第" + j + "行：找不到名字为 " + pareName
												+ " 的上级！");
								dataOk = false;
							}
						}

						if (dataOk) {
							if (mtName != null && !mtName.equals("")) {
								mt = false;
								if (addMap.containsKey(mtName)) {
									mt = true;
								} else {
									for (int arg42 = 0; arg42 < mtList.size(); ++arg42) {
										MakeType arg43 = (MakeType) mtList
												.get(arg42);
										if (arg43.getTypeName().equals(mtName)) {
											mt = true;
											break;
										}
									}
								}

								if (mt) {
									alertMsg = PageHelper.appendAlertMsg(
											alertMsg, "第" + j + "行：名称为 "
													+ mtName + " 的项目类型已经存在！");
									dataOk = false;
								}
							} else {
								alertMsg = PageHelper.appendAlertMsg(alertMsg,
										"第" + j + "行：项目类型名称不能为空！");
								dataOk = false;
							}
						}

						if (dataOk && wcName != null && !wcName.equals("")) {
							mt = false;
							WorkCenter arg44 = null;

							for (k = 0; k < wcList.size(); ++k) {
								arg44 = (WorkCenter) wcList.get(k);
								if (arg44.getWcName().equals(wcName)) {
									mt = true;
									break;
								}
							}

							if (mt) {
								wcId = arg44.getId();
							} else {
								alertMsg = PageHelper.appendAlertMsg(alertMsg,
										"第" + j + "行：找不到名字为 " + wcName
												+ " 的工作中心！");
								dataOk = false;
							}
						}

						if (dataOk) {
							MakeType arg45 = new MakeType();
							arg45.setAutoStoc("N");
							arg45.setId(this.idGenerator.getId(MakeType.class
									.getName()));
							arg45.setParentId(pareId);
							arg45.setParentIdStr(pareIdStr);
							arg45.setSequCode(sequCode);
							arg45.setTnSpell(PageHelper.getPinYinHeadChar(
									mtName).toUpperCase());
							arg45.setType(1);
							arg45.setTypeName(mtName);
							arg45.setWorkCenterId(wcId);
							this.makeTypeDao.save(arg45);
							++count;
							addMap.put(mtName, arg45);
						}
					}
				}
			}

			String arg41 = "<B>成功导入" + count + "行</B>";
			if (alertMsg != null && !alertMsg.equals("")) {
				alertMsg = PageHelper.appendAlertMsg(arg41, alertMsg);
			} else {
				alertMsg = arg41;
			}
		} catch (Exception arg38) {
			alertMsg = "导入错误：" + arg38.getMessage();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception arg37) {
				;
			}

		}

		EncCommonHelper.setAlertMessage(request, alertMsg);
		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		ServletOutputStream arg40 = response.getOutputStream();
		arg40.write("OK".getBytes());
		arg40.flush();
		arg40.close();
		return null;
	}

	public ModelAndView handleGetMakeType(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String name = request.getParameter("mt_name");
		MakeType mt = this.makeTypeDao.getByName(name, true);
		long mtId = 0L;
		long wcId = 0L;
		String mtName = "";
		String wcName = "";
		if (mt != null) {
			mtId = mt.getId();
			mtName = mt.getTypeName();
			wcId = mt.getWorkCenterId();
			if (wcId != 0L) {
				WorkCenter map = (WorkCenter) this.makeTypeDao.get(
						WorkCenter.class, wcId);
				if (map != null) {
					wcName = map.getWcName();
				}
			}
		}

		HashMap map1 = new HashMap();
		map1.put("mtId", Long.valueOf(mtId));
		map1.put("mtName", mtName);
		map1.put("wcId", Long.valueOf(wcId));
		map1.put("wcName", wcName);
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(map1);
		ServletOutputStream out = response.getOutputStream();
		out.write(json.getBytes("utf-8"));
		out.flush();
		out.close();
		return null;
	}

	public String getListView() {
		return this.listView;
	}

	public void setListView(String listView) {
		this.listView = listView;
	}

	public MakeTypeDao getMakeTypeDao() {
		return this.makeTypeDao;
	}

	public void setMakeTypeDao(MakeTypeDao makeTypeDao) {
		this.makeTypeDao = makeTypeDao;
	}

	public String getListForSelectView() {
		return this.listForSelectView;
	}

	public void setListForSelectView(String listForSelectView) {
		this.listForSelectView = listForSelectView;
	}

	public WorkCenterDao getWorkCenterDao() {
		return this.workCenterDao;
	}

	public void setWorkCenterDao(WorkCenterDao workCenterDao) {
		this.workCenterDao = workCenterDao;
	}
}